﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Ninject.Web.Common;
using Site.Infrastructure.Logging;
using Ninject;
using WikiGame.Infrastructure.Ninject;
using WikiGame.Infrastructure.Authorization;
using System.Web.Security;
using WikiGame.Utilities;

namespace WikiGame
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : NinjectHttpApplication
    {
        private ILogger logger;
        public ILogger Logger
        {
            get
            {
                if (logger == null)
                {
                    logger = Container.Get<ILogger>();
                }
                return logger;
            }
        }

        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(Container.Get<IExceptionFilter>());
            filters.Add(new UserAuthorize());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

        }

        protected override void OnApplicationStarted()
        {
            base.OnApplicationStarted();

            AreaRegistration.RegisterAllAreas();
            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
            Logger.Info("Application Started");
        }

        protected override void OnApplicationStopped()
        {
            base.OnApplicationStopped();
            Logger.Info("Application Stopped!");
        }

/*        protected void Session_Start(object sender, EventArgs e)
        {
            if (User.Identity.IsAuthenticated)
            {
                UserInformation ui = new UserInformation();
                ui.UserName = User.Identity.Name;
                LoggedUsersInformation.AddUserInfo(User.Identity.Name, ui);
            }
        }

        protected void Session_End(object sender, EventArgs e)
        {
            LoggedUsersInformation.RemoveUserInfoFor(User.Identity.Name);
        }
*/


        #region Ninject

        protected override IKernel CreateKernel()
        {
            return Container;
        }

        private static IKernel _container;
        public static IKernel Container
        {
            get
            {
                if (_container == null)
                {
                    _container = new StandardKernel(new AppModule());
                    InjectProperties();
                }
                return _container;
            }
        }

        private static void InjectProperties()
        {
            Container.Inject(Membership.Provider);
        }

        #endregion
    }
}